﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyDuAn.BUS;
using System.Data.SqlClient;

namespace QuanLyDuAn.GUI
{
    public partial class FrmInterviewer : DevComponents.DotNetBar.Office2007Form
    {
        InterviewerController controller = new InterviewerController();
        PermissionController pcontroller = new PermissionController();
        DataSet ds,ds1;
        public FrmInterviewer()
        {
            InitializeComponent();

        }

        public void LoadForm() 
        {
            ds = controller.LoadInterviewer();
            ds1 = pcontroller.LoadPermission();
            dataGridViewX1.DataSource = ds.Tables[0];
            cmbPermission.DataSource = ds1.Tables[0];
            cmbPermission.DisplayMember = ds1.Tables[0].Columns[1].ToString();
            cmbPermission.ValueMember = ds1.Tables[0].Columns[0].ToString();
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }
        public bool checkEmpty()
        {
            bool check;
            if (txtFullName.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("FullName is not Empty");
            }
            else if (txtID.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Interview ID is not Empty! ");
            }
            else if (txtUserName.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Username must be input !");
            }
            else if (txtPassword.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Password must be input!");
            }
            else if (cmbDepartment.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Please choose Department");
            }
            else if (cmbPermission.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Please choose Permission");
            }
            else
            {
                check = true;
            }
            return check;
        }
        public void ResetForm()
        {
            txtUserName.Text = "";
            txtID.Text = "";
            txtFullName.Text = "";
            txtPassword.Text = "";
        }
        private void FrmInterviewer_Load(object sender, EventArgs e)
        {
            cmbDepartment.Items.Add("Software I");
            cmbDepartment.Items.Add("Software II");
            cmbDepartment.Items.Add("Software III");
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.LoadForm();
        }
        public bool CheckExist()
        {
            bool check;
            String iId = txtID.Text;
            DataTable dt = new DataTable();
            ds = new DataSet();
            ds = controller.CheckInterviewer(iId);
            dt = ds.Tables[0];
            if (dt.Rows.Count != 0)
            {
                check = false;
                MessageBox.Show("Interviewer is Exits !");
            }
            else
            {
                check = true;
            }
            return check;
        }
        public void InsertInterviewer()
        {
            String id = txtID.Text;
            String username = txtUserName.Text;
            String password = txtPassword.Text;
            String fullName = txtFullName.Text;
            String department =(String)cmbDepartment.SelectedItem;
            int permission = (int)(cmbPermission.SelectedValue);
            controller.InsertInterviewer(id, username, password, fullName, department, permission);
            MessageBox.Show("Insert successful !!!");
        }
        public void UpdateInterviewer()
        {   
            String id = txtID.Text;
            String username = txtUserName.Text;
            String password = txtPassword.Text;
            String fullName = txtFullName.Text;
            String department = (String)cmbDepartment.SelectedItem;
            int permission = (int)cmbPermission.SelectedValue;
            controller.UpdateInterviewer(id, username, password, fullName, department, permission);
            MessageBox.Show("Update successfull");
        }
     
        private void buttonX1_Click(object sender, EventArgs e)
        {
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if ((this.checkEmpty() == true) && (this.CheckExist()==true))
            {
                this.InsertInterviewer();
                this.LoadForm();
                this.ResetForm();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            this.UpdateInterviewer();
            this.LoadForm();
            this.ResetForm();
        }
       

        private void dataGridViewX1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            DataGridViewRow row = new DataGridViewRow();
            row = dataGridViewX1.Rows[e.RowIndex];
            txtID.Text = row.Cells[0].Value.ToString();
            txtUserName.Text = row.Cells[1].Value.ToString();
            txtPassword.Text = row.Cells[2].Value.ToString();
            txtFullName.Text = row.Cells[3].Value.ToString();
            cmbDepartment.Text = row.Cells[4].Value.ToString();
            cmbPermission.Text = row.Cells[5].Value.ToString();
            txtID.Enabled = false;
            
        }

        private void btnDelete_Click_1(object sender, EventArgs e)
        {  
            {
                if (MessageBox.Show("You really want to delete?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    try
                    {
                        String id = txtID.Text;
                        controller.DelInterviewer(id);
                        MessageBox.Show("Del successfull", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadForm();
                        ResetForm();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Errors");
                    }
                }
            }
        }

       
       
    }
}
